package com.dianrui.agent.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.PowerExchangeAdapter;
import com.dianrui.agent.bean.Charge;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 换电人员
 */
public class PowerExchangeActivity extends BaseActivity {
    private PowerExchangeAdapter powerExchangeAdapter;
    private List<Charge> chargeList = new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.activity_power_exchange;
    }

    @Override
    public void initView() {
        title.setText("换电人员");
        powerExchangeAdapter=new PowerExchangeAdapter(PowerExchangeActivity.this,chargeList);
        listview.setAdapter(powerExchangeAdapter);
        getCharing();
        powerExchangeAdapter.setOnPowerExchangeListener(new PowerExchangeAdapter.PowerExchangeListener() {
            @Override
            public void clickMain(String admin_id) {
                Intent intent=new Intent(PowerExchangeActivity.this,ExchangePowerRecordActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("adminId",admin_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


    }

    private void getCharing() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.CHRAING, "", new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null!=jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (chargeList != null) {
                                    chargeList.clear();
                                }
                                JSONArray data = jsonObject.optJSONArray("data");
                                if (data.length() > 0) {
                                    for (int i = 0; i < data.length(); i++) {
                                        Charge charge = new Charge();
                                        charge.admin_id = data.optJSONObject(i).optString("admin_id");
                                        charge.nickname = data.optJSONObject(i).optString("nickname");
                                        charge.month_count = data.optJSONObject(i).optString("month_count");
                                        charge.day_count = data.optJSONObject(i).optString("day_count");
                                        charge.month_effective = data.optJSONObject(i).optString("month_effective");
                                        charge.day_effective = data.optJSONObject(i).optString("day_effective");
                                        charge.month_invalid = data.optJSONObject(i).optString("month_invalid");
                                        charge.day_invalid = data.optJSONObject(i).optString("day_invalid");
                                        chargeList.add(charge);
                                    }
                                    if (powerExchangeAdapter != null) {
                                        powerExchangeAdapter.refresh(chargeList);
                                    }
                                }
                            }else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (chargeList.size()<=0) {
                        emptyView.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    }else {
                        emptyView.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (chargeList != null) {
            chargeList.clear();
        }
    }
}
